﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace CivitaiAPI
{
    public class ModelsResult
    {
        public List<ModelInfo> items { get; set; }
        public ResultMeta metadata { get; set; }
    }

    //public class ModelItem
    //{
    //    public int id { get; set; }
    //    public string name { get; set; }
    //    public string description { get; set; }
    //    public string type { get; set; }
    //    public bool poi { get; set; }
    //    public bool nsfw { get; set; }
    //    public bool allowNoCredit { get; set; }
    //    public string allowCommercialUse { get; set; }
    //    public bool allowDerivatives { get; set; }
    //    public bool allowDifferentLicense { get; set; }
    //    public Stats stats { get; set; }
    //    public Creator creator { get; set; }
    //    public List<string> tags { get; set; }
    //    public List<ModelVersion> modelVersions { get; set; }
    //}

    public class ModelInfo
    {
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string type { get; set; }
        public bool poi { get; set; }
        public bool nsfw { get; set; }
        public bool allowNoCredit { get; set; }
        public string allowCommercialUse { get; set; }
        public bool allowDerivatives { get; set; }
        public bool allowDifferentLicense { get; set; }
        public Stats stats { get; set; }
        public Creator creator { get; set; }
        public List<string> tags { get; set; }
        public List<ModelVersion> modelVersions { get; set; }
    }

    public class ResultMeta
    {
        public int totalItems { get; set; }
        public int currentPage { get; set; }
        public int pageSize { get; set; }
        public int totalPages { get; set; }
        public string nextPage { get; set; }
    }


    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Creator
    {
        public string username { get; set; }
        public string image { get; set; }
    }

    public class ModelFile
    {
        public string name { get; set; }
        public int id { get; set; }
        public double sizeKB { get; set; }
        public string type { get; set; }
        public ModelMeta metadata { get; set; }
        public string pickleScanResult { get; set; }
        public string pickleScanMessage { get; set; }
        public string virusScanResult { get; set; }
        public DateTime? scannedAt { get; set; }
        public Hashes hashes { get; set; }
        public string downloadUrl { get; set; }
        public bool? primary { get; set; }
    }

    public class Hashes
    {
        public string AutoV1 { get; set; }
        public string AutoV2 { get; set; }
        public string SHA256 { get; set; }
        public string CRC32 { get; set; }
        public string BLAKE3 { get; set; }
    }

    public class Image
    {
        public string url { get; set; }
        public string nsfw { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string hash { get; set; }
        public PhotoMeta meta { get; set; }

        public bool HasMeta()
        {
            return meta != null;
        }

        public string FormatMeta()
        {
            return meta?.FormatMeta();
        }
    }

    public class PhotoMeta
    {
        public string FormatMeta()
        {
            string promptOut = (prompt != null ? $"{prompt}\n" : "");
            string negOut = (negativePrompt != null ? $"Negative prompt: {negativePrompt}\n" : "");
            string stepsOut = $"Steps: {steps}";
            string samplerOut = $", Sampler: {sampler}";
            string cfgOut = $", CFG scale: {cfgScale}";
            string seedOut = $", Seed: {seed}";
            string sizeOut = $", Size: {Size}";
            string hashOut = Modelhash != null ? $", Model hash: {Modelhash}" : "";
            string denoiseOut = Denoisingstrength != null ? $", Denoising strength: {Denoisingstrength}" : "";
            string ensdOut = ENSD != null ? $", ENSD: {ENSD}" : "";
            string hiresupscaleOut = Hiresupscale != null ? $", Hires upscale: {Hiresupscale}" : "";
            string hiresstepsOut = Hiressteps != null ? $", Hires steps: {Hiressteps}" : "";
            string hiresupscalerOut = Hiresupscaler != null ? $", Hires upscaler: {Hiresupscaler}" : "";
            return $"{promptOut}{negOut}{stepsOut}{samplerOut}{cfgOut}{seedOut}{sizeOut}{hashOut}{denoiseOut}{ensdOut}{hiresupscaleOut}{hiresstepsOut}{hiresupscalerOut}";
        }

        public string ENSD { get; set; }
        public string Size { get; set; }
        public string seed { get; set; }
        public string steps { get; set; }
        public string prompt { get; set; }
        public string sampler { get; set; }
        public float cfgScale { get; set; }
        public List<Resource> resources { get; set; }

        [JsonPropertyName("Model hash")]
        public string Modelhash { get; set; }

        [JsonPropertyName("Hires upscale")]
        public string Hiresupscale { get; set; }

        [JsonPropertyName("Hires upscaler")]
        public string Hiresupscaler { get; set; }
        public string negativePrompt { get; set; }

        [JsonPropertyName("Denoising strength")]
        public string Denoisingstrength { get; set; }

        [JsonPropertyName("Hires steps")]
        public string Hiressteps { get; set; }
    }

    public class ModelMeta
    {
        public string fp { get; set; }
        public string size { get; set; }
        public string format { get; set; }
    }

    public class ModelVersion
    {
        public int id { get; set; }
        public int modelId { get; set; }
        public string name { get; set; }
        public DateTime createdAt { get; set; }
        public DateTime updatedAt { get; set; }
        public List<string> trainedWords { get; set; }
        public string baseModel { get; set; }
        public int earlyAccessTimeFrame { get; set; }
        public string description { get; set; }
        public Stats stats { get; set; }
        public List<ModelFile> files { get; set; }
        public List<Image> images { get; set; }
        public string downloadUrl { get; set; }
    }

    public class Resource
    {
        public string name { get; set; }
        public string type { get; set; }
        public double? weight { get; set; }
    }

    public class Stats
    {
        public int downloadCount { get; set; }
        public int favoriteCount { get; set; }
        public int commentCount { get; set; }
        public int ratingCount { get; set; }
        public double rating { get; set; }
    }

    public class Tag
    {
        public string name { get; set; }
    }



}
